var app = getApp()

Page({
  data: {
    goodsCode:"",
    goodsName: "铅笔岛 校园三件套 (10人份)",
    freePost:0,
    price:29,
    orderPrice:0,
    buyNum: 1,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    windowWidth:300
  },
  onShareAppMessage: function () {
    var that = this;

    return {
      title: that.data.goodsName,
      path: '/pages/goods/goods?goodsCode='+that.data.goodsCode,
      success: function (res) {
        // 分享成功
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },
  minus: function (event) {
    var that = this
    var buyNum = that.data.buyNum;
    if (buyNum <= 1)
      return;
    buyNum = buyNum - 1;
    var orderPrice = (that.data.price*buyNum).toFixed(2);
    that.setData({
      orderPrice:orderPrice,
      buyNum: buyNum
    });
  },
  plus: function (event) {
    var that = this
    var buyNum = that.data.buyNum;

    buyNum = buyNum + 1;
    var orderPrice = (that.data.price*buyNum).toFixed(2);
    that.setData({
      orderPrice:orderPrice,
      buyNum: buyNum
    });
  },
  buy: function (event) {
    var that = this;

    var payData = {};
    payData.goodsCode = that.data.goodsCode;
    payData.buyNum = that.data.buyNum;
    payData.tradeType = 'JSAPI';
    payData.nickName = app.globalData.userInfo.nickName;

    wx.setStorageSync("payData",payData);

    wx.navigateTo({
      url: '../confirm/confirm'
    })
  },
  onLoad: function (object) {
    var that = this

    app.checkLogin(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    });

    if(!app.globalData.userInfo){
      app.getUserInfo(function(userInfo){
            //更新数据
            that.setData({
              userInfo:userInfo
            })
          })
    }

    var systemInfo = wx.getSystemInfoSync()
    that.setData({
      windowWidth:systemInfo.windowWidth
    });


    wx.request({
          url: app.globalData.serverUrl+'/app/mp', 
          method:'post',
          dataType:'json',
          data: {
            messageId: 'queryGoods',
            body: {
              goodsCode:object.goodsCode
            }
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
           if(res.data.success){
             var imgDetails = res.data.imgDetails;
             for(var i=0;i<imgDetails.length;i++){
                imgDetails[i]['width']=systemInfo.windowWidth
                imgDetails[i]['height']=systemInfo.windowWidth
             }
             that.setData({
                goodsCode:res.data.goodsCode,
                goodsName: res.data.name,
                price:res.data.price,
                orderPrice:res.data.price,
                freePost:res.data.freePost,
                imgUrls:imgDetails
              });
             wx.setNavigationBarTitle({
               title: res.data.name
             })
           }
          }
        })
    
  }
})